package com.googlecode.chigs.web.doku.version;

import com.googlecode.chigs.domain.Product;
import com.googlecode.chigs.domain.Version;
import com.googlecode.chigs.repository.ChigsFacade;
import com.googlecode.chigscommons.web.common.IconFactory;
import com.googlecode.chigscommons.web.util.ComponentUtil;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.List;

/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 */
public class VersionSelector extends Panel {
	@SpringBean
	private ChigsFacade chigs;
	private final PropertyModel<Version> propModel;
	private final static String CREATE_ID = "create";

	public VersionSelector(String id, PropertyModel<Version> model, final Product product) {
		super(id, model);
		this.propModel = model;

		add(createDropDownVersion(product));

		final EditOrCreateVersionWindow editOrCreateVersionWindow = new EditOrCreateVersionWindow("modalwindow", new Model<Version>(new Version()), product){
			@Override
			protected void afterCanel(AjaxRequestTarget target) {
				final DropDownChoice<Version> createDropDownVersion = createDropDownVersion(product);
				VersionSelector.this.addOrReplace(createDropDownVersion);
				target.addComponent(createDropDownVersion);
				close(target);
			}
			@Override
			protected void afterSubmit(AjaxRequestTarget target, IModel<Version> model) {
				final DropDownChoice<Version> createDropDownVersion = createDropDownVersion(product);
				propModel.setObject(model.getObject());
				model.setObject(new Version());
				VersionSelector.this.addOrReplace(createDropDownVersion);
				target.addComponent(createDropDownVersion);
				Component fp = ComponentUtil.getComponentByClass(getPage(), FeedbackPanel.class);
				if (fp!=null) {
					target.addComponent(fp);
				}
				close(target);
			}
		};
		add(editOrCreateVersionWindow);

		AjaxLink<Void> ajaxLink = new AjaxLink<Void>(CREATE_ID){
			public void onClick(AjaxRequestTarget target) {
				editOrCreateVersionWindow.show(target);
			}
		};
		ajaxLink.add(new Image("icon", IconFactory.createOkIconReference()));
		add(ajaxLink);
		setRenderBodyOnly(true);
	}

	private DropDownChoice<Version> createDropDownVersion(Product product) {
		List<Version> versions = chigs.loadVersionsByProduct(product);
		DropDownChoice<Version> dd = new DropDownChoice<Version>("version", propModel, versions, new ChoiceRenderer<Version>("version", "id"));
		dd.add(new AjaxFormComponentUpdatingBehavior("onchange") {
			protected void onUpdate(AjaxRequestTarget target) {
				onChange(target);
			}

		});
		dd.setOutputMarkupId(true);
		dd.setRequired(true);
		return dd;
	}

	/**
	 * Model is updated, if we need more, do it here.
	 * 
	 * override if needed
	 * @param target
	 */
	protected void onChange(AjaxRequestTarget target) {
	}

}
